package com.qingke.example;

import java.io.IOException;
import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/QuitServlet")
public class QuitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QuitServlet() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HashSet<String> usernames=(HashSet<String>)request.getServletContext().getAttribute("userList");
		if(usernames==null){
			return ;
		}
		String name=(String)request.getSession().getAttribute("currentUser");
		boolean success=usernames.remove(name);
		if(success)
			response.getWriter().append("success");
		else
			response.getWriter().append("failed");
			
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
